#!/usr/bin/env bash
#get wifi device
DEVICE=$(ls /sys/class/net | grep ^w)
if [[ ! -n $DEVICE ]]; then
    DEVICE=$(ls /sys/class/net | grep -v lo | head -1)
fi
STATUS=$(nmcli | grep $DEVICE | awk 'FNR == 1 {print $2}')
SSID=$(iwgetid | awk -F '"' '{ print $2 }')
STRENGTH=$(awk 'NR==3 {printf("%.0f",$3*10/7)}' /proc/net/wireless)

toggle() {
    if [[ $STATUS == "已连接" || $STATUS == "connected" || $STATUS == "到" ]]; then
        nmcli radio wifi off
        notify-send --icon=$HOME/tool/wifi.jpeg --urgency=low "Wi-Fi" "Wi-Fi has been turned off!"
    else
        nmcli radio wifi on
        notify-send --icon=$HOME/tool/wifi.jpeg --urgency=low "Wi-Fi" "Wi-Fi has been turned on, you are back online!"
    fi
}

icon() {
    if [[ "$STRENGTH" -ge "80" ]]; then
        echo "󰤨"
    elif [[ "$STRENGTH" -ge "60" ]]; then
        echo "󰤥"
    elif [[ "$STRENGTH" -ge "40" ]]; then
        echo "󰤢"
    elif [[ "$STRENGTH" -ge "20" ]]; then
        echo "󰤟"
    elif [[ "$STRENGTH" -ge "0" ]]; then
        echo "󰤯"
    else
        echo "󰤭"
    fi
}

status() {
    if [[ $STATUS == "已连接" || $STATUS == "connected" || $STATUS == "到" ]]; then
        echo $SSID
    else
        echo Disconnected
    fi
}

class(){
    if [[ $STATUS == "已连接" || $STATUS == "connected" || $STATUS == "到" ]]; then
        echo "󰤨"
    else
        echo "󰤭"
    fi
}

if [[ $1 == "--toggle" ]]; then
    toggle
elif [[ $1 == "--icon" ]]; then
    icon
elif [[ $1 == "--status" ]]; then
    status
elif [[ $1 == "--class" ]]; then
    class
fi

